import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch, Tag } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { setVersionNew } from '/@/api/app/version';
import { appUpdateType } from '/@/enums/constEnum';
export const columns: BasicColumn[] = [
	{
		title: '版本名称',
		dataIndex: 'versionName',
		width: 200
	},
	{
		title: '版本号',
		dataIndex: 'versionCode',
		width: 180
	},
	{
		title: '更新类型',
		dataIndex: 'updateType',
		width: 180,
		customRender: ({ record }) => {
			const text = appUpdateType.find(item => item.value == record.updateType);
			return h(Tag, { color: 'green' }, () => text.label);
		}
	},
	{
		title: '是否最新版本',
		dataIndex: 'isNew',
		width: 180,
		customRender: ({ record }) => {
			if (!Reflect.has(record, 'pendingStatus')) {
				record.pendingStatus = false;
			}
			return h(Switch, {
				checked: record.isNew == '1',
				checkedChildren: '停用',
				unCheckedChildren: '启用',
				loading: record.pendingStatus,
				onChange(checked: boolean) {
					record.pendingStatus = true;
					const newStatus = checked ? '1' : '0';
					const { createMessage } = useMessage();
					setVersionNew(record.id, newStatus)
						.then(() => {
							record.isNew = newStatus;
							createMessage.success(`已成功修改状态`);
						})
						.catch(() => {
							createMessage.error('修改状态失败');
						})
						.finally(() => {
							record.pendingStatus = false;
						});
				}
			});
		}
	},
	{
		title: '备注',
		dataIndex: 'remark'
	}
];

export const searchFormSchema: FormSchema[] = [
	{
		field: 'F_versionName_like',
		label: '版本名称',
		component: 'Input',
		componentProps: {
			placeholder: '请输入版本名称'
		},
		colProps: { span: 6 }
	},
	{
		field: 'ORDER_isNew_desc',
		label: '',
		defaultValue:1,
		component: 'Input',
		show: false
	}
];

export const formSchema: FormSchema[] = [
	{
		field: 'id',
		label: 'ID',
		component: 'Input',
		show: false
	},
	{
		field: 'versionName',
		label: '版本名称',
		required: true,
		component: 'Input'
	},
	{
		field: 'versionCode',
		label: '版本号',
		required: true,
		component: 'InputNumber'
	},
	{
		field: 'updateType',
		label: '更新类型',
		component: 'RadioButtonGroup',
		defaultValue: 'forcibly',
		required: true,
		componentProps: {
			options: [...appUpdateType]
		},
		colProps: { lg: 24, md: 24 }
	},
	{
		field: 'androidDownloadUrl',
		label: 'Android下载地址',
		component: 'Input'
	},
	{
		field: 'iosDownloadUrl',
		label: '苹果下载地址',
		component: 'Input'
	},
	{
		field: 'versionDesc',
		label: '版本说明',
		required: true,
		component: 'InputTextArea'
	},
	{
		label: '备注',
		field: 'remark',
		component: 'InputTextArea'
	}
];
